<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <ContentWrap>
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="100px"
      >
        <el-form-item label="设备名称" prop="assetName">
          <el-input
            v-model="queryParams.assetName"
            placeholder="请输入资产名称"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="安装位置" prop="location">
          <el-input
            v-model="queryParams.location"
            placeholder="请输入安装位置"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="设备状态" prop="deviceStatus">
          <el-select
            v-model="queryParams.deviceStatus"
            placeholder="请选择设备状态"
            clearable
            class="!w-240px"
          >
            <el-option
              v-for="dict in getIntDictOptions(DICT_TYPE.BMS_DEVICE_STATUS)"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
          <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
          <el-button
            type="primary"
            plain
            @click="openForm('create')"
            v-hasPermi="['bms:device:create']"
          >
            <Icon icon="ep:plus" class="mr-5px" /> 新增
          </el-button>
          <el-button
            type="primary"
            plain
            @click="syncAsset()"
            v-hasPermi="['bms:device:create']"
          >
            <Icon icon="ep:refresh" class="mr-5px" /> 资产同步
          </el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>

    <!-- 列表 -->
    <ContentWrap>
      <el-table v-loading="loading" :data="deviceList">
        <el-table-column label="设备编码" align="center" prop="assetSn" />
        <el-table-column label="设备名称" align="center" prop="assetName" />
        <el-table-column label="设备位置" align="center" prop="location" />
        <el-table-column label="设备品牌" align="center" prop="assetBrand" />
        <el-table-column label="设备型号" align="center" prop="assetModel" />
        <el-table-column label="设备状态" align="center" prop="deviceStatus">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.BMS_DEVICE_STATUS" :value="scope.row.deviceStatus" />
          </template>
        </el-table-column>
        <el-table-column
          label="安装时间"
          align="center"
          prop="installDate"
          width="180"
          :formatter="dateFormatter2"
        />
        <el-table-column label="验收状态" align="center" prop="acceptanceStatus">
          <template #default="scope">
            <dict-tag
              :type="DICT_TYPE.BMS_DEVICE_ACCEPTANCE_STATUS"
              :value="scope.row.acceptanceStatus"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="验收时间"
          align="center"
          prop="acceptanceDate"
          width="180"
          :formatter="dateFormatter2"
        />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip class="box-item" effect="dark" content="编辑" placement="top">
              <el-button link type="primary" @click="openForm('update', scope.row.id)">
                <Icon icon="ep:edit" />
              </el-button>
            </el-tooltip>
            <el-divider direction="vertical" />
            <el-tooltip class="box-item" effect="dark" content="配件" placement="top">
              <el-button link type="primary" icon="Setting" @click="handleConfig(scope.row.id)" />
            </el-tooltip>
            <el-divider direction="vertical" />
            <el-tooltip class="box-item" effect="dark" content="删除" placement="top">
              <el-popconfirm title="是否确认移除该设备的运行管理？" @confirm="handleDelete(scope.row)">
                <template #reference>
                  <el-button link type="danger">
                    <Icon icon="ep:delete" />
                  </el-button>
                </template>
              </el-popconfirm>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>

    <!-- 添加或修改用户对话框 -->
    <DeviceForm ref="formRef" @success="getList" />
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive } from 'vue'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { ElMessage } from 'element-plus'
import { dateFormatter2 } from '@/utils/formatTime'
import * as DeviceApi from '@/api/bms/device'
import DeviceForm from './DeviceForm.vue'

defineOptions({ name: 'BmsDevice' })

const queryFormRef = ref()

const loading = ref(false)
const total = ref(0)
const deviceList = ref([])

// 查询参数扩展
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  assetName: '',
  location: '',
  deviceStatus: undefined,
  assetSn: ''
})

// 重置查询参数
const resetQuery = () => {
  queryFormRef.value?.resetFields()
  handleQuery()
}

// 搜索按钮操作
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

// 获取资产列表
const getList = async () => {
  loading.value = true
  try {
    const data = await DeviceApi.getDevicePage(queryParams)
    deviceList.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

const syncAsset = async () => {
  await DeviceApi.syncAsset().then(() => {
    getList()
    ElMessage.success('同步成功')
  })
}
// 删除按钮操作
const handleDelete = async (row) => {
  const id = row.id
  await DeviceApi.deleteDevice(id).then(() => {
    getList()
    ElMessage.success('删除成功')
  })
}

// 跳转配置
const router = useRouter()
const handleConfig = (id: string) => {
  if (!id) return
  router.push(`fittingDeviceConfig/${id}`)
}
getList()
</script>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 24px;
  margin-bottom: 16px;
  background: #f8f9fa;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.card-header:hover {
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  transform: translateY(-2px);
}

@media (max-width: 768px) {
  .card-header {
    padding: 12px;
    flex-direction: column;
    gap: 8px;
  }
}

.el-select {
  width: 100%;
}
</style>
